package me.may.money.api;

import java.util.LinkedHashMap;
import java.util.List;

import org.bukkit.OfflinePlayer;

import me.may.money.service.MoneyService;
import me.may.money.service.MoneyServiceImpl;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;

/**
 * 交子API, 已注册Vault必要功能
 * 
 * @author May_Speed
 */
public class MoneyAPI implements Economy {

	private static MoneyService moneyService = new MoneyServiceImpl();
	
	/**
	 * 取玩家的交子
	 * 
	 * @param name
	 *            玩家名
	 * @return 交子数量
	 */
	public static double getPlayerMoney(String name) {
		return moneyService.getPlayerMoney(name);
	}

	/**
	 * 给一名玩家的账户添加一定数量的交子
	 * 
	 * @param name
	 *            玩家名
	 * @param amount
	 *            交子数量
	 */
	public static void addPlayerMoney(String name, double amount) {
		moneyService.addPlayerMoney(name, amount);
	}

	/**
	 * 给一名玩家的账户拿走一定数量的交子
	 * 
	 * @param name
	 *            玩家名
	 * @param amount
	 *            交子数量
	 */
	public static void takePlayerMoney(String name, double amount) {
		moneyService.takePlayerMoney(name, amount);
	}

	/**
	 * 给一名玩家的账户设置一定数量的交子
	 * 
	 * @param name
	 *            玩家名
	 * @param amount
	 *            交子数量
	 */
	public static void setPlayerMoney(String name, double amount) {
		moneyService.setPlayerMoney(name, amount);
	}

	/**
	 * 初始化玩家账户
	 * 
	 * @param name
	 *            玩家名
	 */
	public static void initialPlayerAccount(String name) {
		moneyService.initialPlayerAccount(name);
	}

	/**
	 * 判断一名玩家的是否拥有足够的交子
	 * 
	 * @param name
	 *            玩家名
	 * @param amount
	 *            交子数量
	 * @return true[拥有]/false[未拥有]
	 */
	public static boolean hasEnoughMoney(String name, double amount) {
		return moneyService.hasEnoughMoney(name, amount);
	}

	/**
	 * 判断一名玩家是否拥有交子账户
	 * 
	 * @param name
	 *            玩家名
	 * @return true[有]/false[没有]
	 */
	public static boolean hasMoneyAccount(String name) {
		return moneyService.hasMoneyAccount(name);
	}
	
	/**
	 * 取交子Top
	 * 
	 * @param limit
	 *            数量
	 * @return {@link LinkedHashMap}
	 */
	public static LinkedHashMap<String, Double> getTop(int limit) {
		return moneyService.getTop(limit);
	}

	@Override
	public EconomyResponse bankBalance(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EconomyResponse bankDeposit(String arg0, double arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EconomyResponse bankHas(String arg0, double arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EconomyResponse bankWithdraw(String arg0, double arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EconomyResponse createBank(String arg0, String arg1) {
		
		return null;
	}

	@Override
	public EconomyResponse createBank(String arg0, OfflinePlayer arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean createPlayerAccount(String arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean createPlayerAccount(OfflinePlayer arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean createPlayerAccount(String arg0, String arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean createPlayerAccount(OfflinePlayer arg0, String arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String currencyNamePlural() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String currencyNameSingular() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EconomyResponse deleteBank(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EconomyResponse depositPlayer(String playerName, double amount) {
		if (amount < 0.0D) {
			return new EconomyResponse(0.0D, 0.0D, EconomyResponse.ResponseType.FAILURE, "不能给予负资金");
		}
		addPlayerMoney(playerName, amount);
		return new EconomyResponse(amount, getPlayerMoney(playerName), EconomyResponse.ResponseType.SUCCESS, null);
	}

	@Override
	public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {
		return depositPlayer(player.getName(), amount);
	}

	@Override
	public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
		return depositPlayer(playerName, amount);
	}

	@Override
	public EconomyResponse depositPlayer(OfflinePlayer player, String worldName, double amount) {
		return depositPlayer(player.getName(), amount);
	}

	@Override
	public String format(double amount) {
		return amount + " 两";
	}

	@Override
	public int fractionalDigits() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public double getBalance(String playerName) {
		return getPlayerMoney(playerName);
	}

	@Override
	public double getBalance(OfflinePlayer player) {
		return getBalance(player.getName());
	}

	@Override
	public double getBalance(String playerName, String worldName) {
		return getBalance(playerName);
	}

	@Override
	public double getBalance(OfflinePlayer player, String worldName) {
		return getBalance(player.getName());
	}

	@Override
	public List<String> getBanks() {
		return null;
	}

	@Override
	public String getName() {
		return "交子 ";
	}

	@Override
	public boolean has(String playerName, double amount) {
		return hasEnoughMoney(playerName, amount);
	}

	@Override
	public boolean has(OfflinePlayer player, double amount) {
		return has(player.getName(), amount);
	}

	@Override
	public boolean has(String playerName, String worldName, double amount) {
		return has(playerName, amount);
	}

	@Override
	public boolean has(OfflinePlayer player, String worldName, double amount) {
		return has(player, amount);
	}

	@Override
	public boolean hasAccount(String playerName) {
		return hasMoneyAccount(playerName);
	}

	@Override
	public boolean hasAccount(OfflinePlayer player) {
		return hasAccount(player.getName());
	}

	@Override
	public boolean hasAccount(String playerName, String worldName) {
		return hasAccount(playerName);
	}

	@Override
	public boolean hasAccount(OfflinePlayer player, String worldName) {
		return hasAccount(player.getName());
	}

	@Override
	public boolean hasBankSupport() {
		return false;
	}

	@Override
	public EconomyResponse isBankMember(String arg0, String arg1) {
		return null;
	}

	@Override
	public EconomyResponse isBankMember(String arg0, OfflinePlayer arg1) {
		return null;
	}

	@Override
	public EconomyResponse isBankOwner(String arg0, String arg1) {
		return null;
	}

	@Override
	public EconomyResponse isBankOwner(String arg0, OfflinePlayer arg1) {
		return null;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public EconomyResponse withdrawPlayer(String playerName, double amount) {
		if (amount < 0.0D) {
			return new EconomyResponse(0.0D, 0.0D, EconomyResponse.ResponseType.FAILURE, "无法扣除负资金");
		}
		if (has(playerName, amount)) {
			takePlayerMoney(playerName, amount);
			return new EconomyResponse(amount, getPlayerMoney(playerName), EconomyResponse.ResponseType.SUCCESS, null);
		}
		return new EconomyResponse(0.0D, getPlayerMoney(playerName), EconomyResponse.ResponseType.FAILURE, "资金不足");
	}

	@Override
	public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {
		return withdrawPlayer(player.getName(), amount);
	}

	@Override
	public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
		return withdrawPlayer(playerName, amount);
	}

	@Override
	public EconomyResponse withdrawPlayer(OfflinePlayer player, String worldName, double amount) {
		return withdrawPlayer(player.getName(), amount);
	}
}
